//
//  XLPopNoticeView.m
//  industry
//
//  Created by zxs on 2021/1/29.
//  Copyright © 2021 Haier. All rights reserved.
//

#import "XLPopNoticeView.h"

@interface XLPopNoticeView ()

// 容器视图
@property (nonatomic, strong) XLView *containerView;

// 通知标题
@property (nonatomic, strong) XLLabel *titleLabel;

// 通知内容
@property (nonatomic, strong) XLLabel *contentLabel;

// 确认按钮
@property (nonatomic, strong) XLButton *okButton;

// 按钮标签
@property (nonatomic, strong) XLLabel *okLabel;

// 消息下标
@property (nonatomic, assign) NSInteger noticeIndex;

@end

@implementation XLPopNoticeView

#pragma mark - public
// 弹窗展示通知
+ (void)showNoticeArray:(NSArray<XLPopupModel *> *)noticeArray {
    // 没有通知，不展示通知弹窗
    if ((noticeArray == nil) || (noticeArray.count == 0)) {
        XLLog(@"没有通知，不展示通知弹窗");
        return;
    }
    
    // 将通知视图全屏显示在window上，防止点击；
    XLPopNoticeView *noticView = [[XLPopNoticeView alloc] init];
    noticView.noticeArray = noticeArray;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:noticView];
    [noticView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(0);
    }];
}

#pragma mark - life
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 全屏透明背景，防止点击事件
        self.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.6];
        
        // 容器视图
        [self addSubview:self.containerView];
        
        // 通知标题
        [self.containerView addSubview:self.titleLabel];
        
        // 通知内容
        [self.containerView addSubview:self.contentLabel];
        
        // 确认按钮
        [self.containerView addSubview:self.okButton];
        
        // 按钮标签
        [self.containerView addSubview:self.okLabel];
        
        // 消息下标
        self.noticeIndex = 0;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 消息容器，颜色渐变#FFFFFF ~ #F5F7F9
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(0);
        make.size.equalTo(CGSizeMake(250, 209));
    }];
    
    // 通知标题
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(22);
        make.left.equalTo(27);
    }];
    
    // 通知内容
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(25);
        make.right.equalTo(-25);
        make.top.equalTo(50);
        make.bottom.equalTo(-45);
    }];
    
    // 确认按钮
    [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(0);
        make.height.equalTo(45);
    }];
    
    // 按钮标签
    [self.okLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(-18);
        make.centerX.equalTo(0);
    }];
}

- (void)dealloc {
    XLLog(@"XLPopNoticeView dealloc");
}

#pragma mark - selector
- (void)okButtonClicked {
    // 下标移动，指向吓一跳消息
    self.noticeIndex++;
    
    // 显示完了，直接消除视图
    if (self.noticeIndex >= self.noticeArray.count) {
        [self removeFromSuperview];
        return;
    }
    
    // 显示吓一跳消息；
    XLPopupModel *noticeModel = [self.noticeArray objectAtIndex:self.noticeIndex];
    self.contentLabel.text = noticeModel.body;
    self.titleLabel.text = noticeModel.title;
}

#pragma mark - setter/getter
// 容器视图
- (XLView *)containerView {
    if (!_containerView) {
        _containerView = [[XLView alloc] init];
        // 颜色渐变#FFFFFF ~ #F5F7F9
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = CGRectMake(0, 0, 250, 209);
        gradientLayer.cornerRadius = 12;
        gradientLayer.startPoint = CGPointMake(0.5, 0);
        gradientLayer.endPoint = CGPointMake(0.5, 1);
        gradientLayer.colors = @[(__bridge id)COLOR_WITH_HEX(0xFFFFFF).CGColor, (__bridge id)COLOR_WITH_HEX(0xF5F7F9).CGColor];
        gradientLayer.locations = @[@(0), @(1.0f)];
        [_containerView.layer addSublayer:gradientLayer];
    }
    return _containerView;
}

// 通知标题
- (XLLabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[XLLabel alloc] init];
        _titleLabel.text = @"重要通知";
        _titleLabel.font = [UIFont fontOfHelveticaNeueMediumWithSize:18];
        _titleLabel.textColor = COLOR_WITH_HEX(0x333333);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}

// 通知内容
- (XLLabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[XLLabel alloc] init];
        _contentLabel.text = @"通知内容";
        _contentLabel.font = [UIFont fontOfPingFangSCRegularWithSize:12];
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = COLOR_WITH_HEX(0x333333);
        _contentLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _contentLabel;
}

// 确认按钮
- (XLButton *)okButton {
    if (!_okButton) {
        _okButton = [[XLButton alloc] init];
        _okButton.backgroundColor = [UIColor clearColor];
        [_okButton addTarget:self action:@selector(okButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okButton;
}

// 按钮标签
- (XLLabel *)okLabel {
    if (!_okLabel) {
        _okLabel = [[XLLabel alloc] init];
        _okLabel.text = @"我知道了";
        _okLabel.font = [UIFont fontOfHelveticaNeueMediumWithSize:15];
        _okLabel.textColor = COLOR_WITH_HEX(0x0845C3);
        _okLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _okLabel;
}

// 消息数组
- (void)setNoticeArray:(NSArray<XLPopupModel *> *)noticeArray {
    if ((noticeArray != nil) && (noticeArray.count > 0)) {
        _noticeArray = noticeArray;
        XLPopupModel *noticeModel = [noticeArray firstObject];
        _contentLabel.text = noticeModel.body;
        _titleLabel.text = noticeModel.title;
    }
}

@end
